/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package samoloty.data;

import java.io.File;
import java.io.IOException;
import jxl.*;
import jxl.read.biff.BiffException;

/**
 *
 * @author frankowp
 */
public class ParserBaz {

    private static Workbook workbook;

    public static void main(String[] args) {
        ParserBaz parser = new ParserBaz();
        //for (int i = 1; i <= 13; i++) {
            System.err.print(parser.parseBaseNO(13));
            System.err.print("\n\n\n\n\n\n\n\n\n");
        //}
    }

    private String parseBaseNO(int number) {
        ParserBaz parser = new ParserBaz();
        try {
            workbook = Workbook.getWorkbook(new File("C:\\Documents and Settings\\user_name\\Pulpit\\bazy\\Baza" + number + ".xls"));
            String[] sheetNames = workbook.getSheetNames();
            StringBuilder outsh1 = new StringBuilder("");
            outsh1.append(parser.header(number));

            for (int i = 0; i < sheetNames.length; i++) {
                //System.err.print(sheetNames[i] + "\n");
                Sheet sheet = workbook.getSheet(sheetNames[i]);
                if (sheetNames[i].equals("AC REG")) {
                    outsh1.append(parser.acreg(sheet));
                }
                if (sheetNames[i].equals("CONST")) {
                    outsh1.append(parser.consta(sheet));
                }
                if (sheetNames[i].equals("CREW")) {
                    outsh1.append(parser.crew(sheet));
                }
                if (sheetNames[i].equals("CONFIG")) {
                    outsh1.append(parser.config(sheet));
                }
                if (sheetNames[i].equals("FUEL")) {
                    outsh1.append(parser.fuel(sheet));
                }
                if (sheetNames[i].equals("HOLDS")) {
                    outsh1.append(parser.holds(sheet));
                }
                if (sheetNames[i].equals("LIMITZFW")) {
                    outsh1.append(parser.limitzfw(sheet));
                }
                if (sheetNames[i].equals("LIMITTOW")) {
                    outsh1.append(parser.limittow(sheet));
                }
                if (sheetNames[i].equals("LIMITLAW")) {
                    outsh1.append(parser.limitlaw(sheet));
                }
                if (sheetNames[i].equals("LIMITZFWFF")) {
                    outsh1.append(parser.limitzfwff(sheet));
                }
                if (sheetNames[i].equals("LIMITTOWFF")) {
                    outsh1.append(parser.limittowff(sheet));
                }
                if (sheetNames[i].equals("PANTRY")) {
                    outsh1.append(parser.pantry(sheet));
                }
                if (sheetNames[i].equals("PAX")) {
                    outsh1.append(parser.pax(sheet));
                }
                if (sheetNames[i].equals("MAX WEIGHTS")) {
                    outsh1.append(parser.maxweights(sheet));
                }
                if (sheetNames[i].equals("STAB TRIM FLAP1")) {
                    outsh1.append(parser.stf(sheet,1));
                }
                if (sheetNames[i].equals("STAB TRIM FLAP2")) {
                    outsh1.append(parser.stf(sheet,2));
                }
                if (sheetNames[i].equals("STAB TRIM FLAP3")) {
                    outsh1.append(parser.stf(sheet,3));
                }
                if (sheetNames[i].equals("STAB TRIM FLAP4")) {
                    outsh1.append(parser.stf(sheet,4));
                }
                if (sheetNames[i].equals("IDEAL TRIM")) {
                    outsh1.append(parser.idealtrim(sheet));
                }
            }
            outsh1.append(parser.footer());
            return outsh1.toString();
        } catch (IOException ex) {
            workbook.close();
        } catch (BiffException ex) {
            workbook.close();
        }
        workbook.close();
        return "";
    }

    private String acreg(Sheet sheet) {
        StringBuilder outsh1 = new StringBuilder("private void initAcReg() {\n\tacRegData = new ArrayList<String>();\n\t");
        Cell[] column = sheet.getColumn(0);
        CellType empty = CellType.EMPTY;
        int i = 0;
        while (i < column.length) {
            if (!column[i].getContents().equals("AC-REG")) {
                outsh1.append("acRegData.add(new String(\"").append(column[i].getContents()).append("\"));\n\t");
            }
            i++;
        }
        outsh1.append("\n}\n");
        return outsh1.toString();
    }

    private String consta(Sheet sheet) {
        StringBuilder outsh1 = new StringBuilder("private void initConst() {\n\tconstData = new ConstModel(");
        outsh1.append(sheet.getCell("A2").getContents()).append(", ");
        outsh1.append(sheet.getCell("B2").getContents()).append(", ");
        outsh1.append(ParserBaz.doubleWithDot(sheet.getCell("C2").getContents())).append(", ");
        outsh1.append(ParserBaz.doubleWithDot(sheet.getCell("D2").getContents())).append(", ");
        outsh1.append(ParserBaz.doubleWithDot(sheet.getCell("E2").getContents())).append(", ");
        outsh1.deleteCharAt(outsh1.length() - 2);
        outsh1.append(");\n}\n");
        return outsh1.toString();
    }

    private String crew(Sheet sheet) {
        StringBuilder outsh1 = new StringBuilder("\nprivate void initCrew() {\n\tcrewData = new ArrayList<CrewModel>();\n\t");
        Cell[] columnA = sheet.getColumn(0);
        Cell[] columnB = sheet.getColumn(1);
        Cell[] columnC = sheet.getColumn(2);
        Cell[] columnD = sheet.getColumn(3);
        Cell[] columnE = sheet.getColumn(4);
        CellType empty = CellType.EMPTY;
        int i = 1;
        while (i < columnA.length) {
            outsh1.append("crewData.add(new CrewModel(");
            outsh1.append("\"").append(columnA[i].getContents()).append("\"").append(", ");
            outsh1.append(columnB[i].getContents()).append(", ");
            outsh1.append(columnC[i].getContents()).append(", ");
            outsh1.append(columnD[i].getContents()).append(", ");
            outsh1.append(ParserBaz.doubleWithDot(columnE[i].getContents())).append("));\n\t");
            i++;
        }
        outsh1.append("\n}\n");
        return outsh1.toString();
    }

    private String config(Sheet sheet) {
        StringBuilder outsh1 = new StringBuilder("private void initConfiguratin() {\n\tconfiguration = new ArrayList<ConfigurationModel>();\n\t");
        Cell[] columnA = sheet.getColumn(0);
        Cell[] columnB = sheet.getColumn(1);
        Cell[] columnC = sheet.getColumn(2);
        Cell[] columnD = sheet.getColumn(3);
        Cell[] columnE = sheet.getColumn(4);
        Cell[] columnF = sheet.getColumn(5);
        Cell[] columnG = sheet.getColumn(6);
        Cell[] columnH = sheet.getColumn(7);
        Cell[] columnI = sheet.getColumn(8);
        CellType empty = CellType.EMPTY;
        int i = 1;
        while (i < columnA.length) {
            outsh1.append("configuration.add(new ConfigurationModel(");
            outsh1.append("\"").append(columnA[i].getContents()).append("\"").append(", \t");
            outsh1.append(columnB[i].getContents()).append(", ");
            outsh1.append(columnC[i].getContents()).append(", ");
            outsh1.append(columnD[i].getContents()).append(", ");
            outsh1.append(columnE[i].getContents()).append(", ");
            outsh1.append(columnF[i].getContents()).append(", ");
            outsh1.append(columnG[i].getContents()).append(", ");
            outsh1.append(columnH[i].getContents()).append(", ");
            outsh1.append(columnI[i].getContents()).append("));\n\t");
            i++;
        }
        outsh1.append("\n}\n");
        return outsh1.toString();
    }

    private String fuel(Sheet sheet) {
        StringBuilder outsh1 = new StringBuilder("\nprivate void initFuel() {\n\tfuelData = new ArrayList<FuelModel>();\n\t");
        Cell[] columnA = sheet.getColumn(0);
        Cell[] columnB = sheet.getColumn(1);
        CellType empty = CellType.EMPTY;
        int i = 1;
        while (i < columnA.length) {
            outsh1.append("fuelData.add(new FuelModel(");
            outsh1.append(columnA[i].getContents()).append(", ");
            outsh1.append(ParserBaz.doubleWithDot(columnB[i].getContents())).append("));\n\t");
            i++;
        }
        outsh1.append("\n}\n");
        return outsh1.toString();
    }

    private String holds(Sheet sheet) {
        StringBuilder outsh1 = new StringBuilder("\nprivate void initHolds() {\n\tholdsData = new ArrayList<HoldsModel>();\n\t");
        Cell[] columnA = sheet.getColumn(0);
        Cell[] columnB = sheet.getColumn(1);
        Cell[] columnC = sheet.getColumn(2);
        CellType empty = CellType.EMPTY;
        int i = 1;
        while (i < columnA.length) {
            outsh1.append("holdsData.add(new HoldsModel(");
            outsh1.append("\"").append(columnA[i].getContents()).append("\"").append(", ");
            outsh1.append(columnB[i].getContents()).append(", ");
            outsh1.append(ParserBaz.doubleWithDot(columnC[i].getContents())).append("));\n\t");
            i++;
        }
        outsh1.append("\n}\n");
        return outsh1.toString();
    }

    private String limitzfw(Sheet sheet) {
        StringBuilder outsh1 = new StringBuilder("\nprivate void initLimitZfw() {\n\tlimitZfwData = new ArrayList<LimitZfwModel>();\n\t");
        Cell[] columnA = sheet.getColumn(0);
        Cell[] columnB = sheet.getColumn(1);
        Cell[] columnC = sheet.getColumn(2);
        Cell[] columnD = sheet.getColumn(3);
        CellType empty = CellType.EMPTY;
        int i = 2;
        while (i < columnA.length) {
            outsh1.append("limitZfwData.add(new LimitZfwModel(");
            outsh1.append(columnA[i].getContents()).append(", ");
            outsh1.append(ParserBaz.doubleWithDot(columnB[i].getContents())).append(", ");
            outsh1.append(columnC[i].getContents()).append(", ");
            outsh1.append(ParserBaz.doubleWithDot(columnD[i].getContents())).append("));\n\t");
            i++;
        }
        outsh1.append("\n}\n");
        return outsh1.toString();
    }

    private String limittow(Sheet sheet) {
        StringBuilder outsh1 = new StringBuilder("\nprivate void initLimitTow() {\n\tlimitTowData = new ArrayList<LimitTowModel>();\n\t");
        Cell[] columnA = sheet.getColumn(0);
        Cell[] columnB = sheet.getColumn(1);
        Cell[] columnC = sheet.getColumn(2);
        Cell[] columnD = sheet.getColumn(3);
        CellType empty = CellType.EMPTY;
        int i = 2;
        while (i < columnA.length) {
            outsh1.append("limitTowData.add(new LimitTowModel(");
            outsh1.append(columnA[i].getContents()).append(", ");
            outsh1.append(ParserBaz.doubleWithDot(columnB[i].getContents())).append(", ");
            outsh1.append(columnC[i].getContents()).append(", ");
            outsh1.append(ParserBaz.doubleWithDot(columnD[i].getContents())).append("));\n\t");
            i++;
        }
        outsh1.append("\n}\n");
        return outsh1.toString();
    }

    private String limitlaw(Sheet sheet) {
        StringBuilder outsh1 = new StringBuilder("\nprivate void initLimitLaw() {\n\tlimitLawData = new ArrayList<LimitLawModel>();\n\t");
        Cell[] columnA = sheet.getColumn(0);
        Cell[] columnB = sheet.getColumn(1);
        Cell[] columnC = sheet.getColumn(2);
        Cell[] columnD = sheet.getColumn(3);
        CellType empty = CellType.EMPTY;
        int i = 2;
        while (i < columnA.length) {
            outsh1.append("limitLawData.add(new LimitLawModel(");
            outsh1.append(columnA[i].getContents()).append(", ");
            outsh1.append(ParserBaz.doubleWithDot(columnB[i].getContents())).append(", ");
            outsh1.append(columnC[i].getContents()).append(", ");
            outsh1.append(ParserBaz.doubleWithDot(columnD[i].getContents())).append("));\n\t");
            i++;
        }
        outsh1.append("\n}\n");
        return outsh1.toString();
    }

    private String limitzfwff(Sheet sheet) {
        StringBuilder outsh1 = new StringBuilder("\nprivate void initLimitZfwff() {\n\tlimitZfwffData = new ArrayList<LimitZfwffModel>();\n\t");
        Cell[] columnA = sheet.getColumn(0);
        Cell[] columnB = sheet.getColumn(1);
        Cell[] columnC = sheet.getColumn(2);
        Cell[] columnD = sheet.getColumn(3);
        CellType empty = CellType.EMPTY;
        int i = 2;
        while (i < columnA.length) {
            outsh1.append("limitZfwffData.add(new LimitZfwffModel(");
            outsh1.append(columnA[i].getContents()).append(", ");
            outsh1.append(ParserBaz.doubleWithDot(columnB[i].getContents())).append(", ");
            outsh1.append(columnC[i].getContents()).append(", ");
            outsh1.append(ParserBaz.doubleWithDot(columnD[i].getContents())).append("));\n\t");
            i++;
        }
        outsh1.append("\n}\n");
        return outsh1.toString();
    }

    private String limittowff(Sheet sheet) {
        StringBuilder outsh1 = new StringBuilder("\nprivate void initLimitTowff() {\n\tlimitTowffData = new ArrayList<LimitTowffModel>();\n\t");
        Cell[] columnA = sheet.getColumn(0);
        Cell[] columnB = sheet.getColumn(1);
        Cell[] columnC = sheet.getColumn(2);
        Cell[] columnD = sheet.getColumn(3);
        CellType empty = CellType.EMPTY;
        int i = 2;
        while (i < columnA.length) {
            outsh1.append("limitTowffData.add(new LimitTowffModel(");
            outsh1.append(columnA[i].getContents()).append(", ");
            outsh1.append(ParserBaz.doubleWithDot(columnB[i].getContents())).append(", ");
            outsh1.append(columnC[i].getContents()).append(", ");
            outsh1.append(ParserBaz.doubleWithDot(columnD[i].getContents())).append("));\n\t");
            i++;
        }
        outsh1.append("\n}\n");
        return outsh1.toString();
    }

    private String pantry(Sheet sheet) {
        StringBuilder outsh1 = new StringBuilder("\nprivate void initPantry() {\n\tpantryData = new PantryModel(");
        Cell[] columnA = sheet.getColumn(0);
        Cell[] columnB = sheet.getColumn(1);
        Cell[] columnC = sheet.getColumn(2);
        Cell[] columnD = sheet.getColumn(3);
        Cell[] columnE = sheet.getColumn(4);
        Cell[] columnF = sheet.getColumn(5);
        CellType empty = CellType.EMPTY;
        int i = 1;
        outsh1.append(columnA[i].getContents()).append(", ");
        outsh1.append(ParserBaz.doubleWithDot(columnB[i].getContents())).append(", ");
        outsh1.append(ParserBaz.doubleWithDot(columnC[i].getContents())).append(", ");
        outsh1.append(ParserBaz.doubleWithDot(columnD[i].getContents())).append(", ");
        outsh1.append(columnE[i].getContents()).append(", ");
        outsh1.append(ParserBaz.doubleWithDot(columnF[i].getContents())).append(");\n\t");
        outsh1.append("\n}\n");
        return outsh1.toString();
    }

    private String pax(Sheet sheet) {
        StringBuilder outsh1 = new StringBuilder("\nprivate void initPax() {\n\tpaxData = new ArrayList<PaxModel>();\n\t");
        Cell[] columnA = sheet.getColumn(0);
        Cell[] columnB = sheet.getColumn(1);
        CellType empty = CellType.EMPTY;
        int i = 1;
        while (i < columnA.length) {
            outsh1.append("paxData.add(new PaxModel(");
            outsh1.append("\"").append(columnA[i].getContents()).append("\"").append(", ");
            outsh1.append(ParserBaz.doubleWithDot(columnB[i].getContents())).append("));\n\t");
            i++;
        }
        outsh1.append("\n}\n");
        return outsh1.toString();
    }

    private String maxweights(Sheet sheet) {
        StringBuilder outsh1 = new StringBuilder("\nprivate void initMaxWeights() {\n\tmaxWeightsData = new MaxWeightsModel(");
        Cell[] columnA = sheet.getColumn(0);
        Cell[] columnB = sheet.getColumn(1);
        Cell[] columnC = sheet.getColumn(2);
        Cell[] columnD = sheet.getColumn(3);
        CellType empty = CellType.EMPTY;
        int i = 1;
        outsh1.append(columnA[i].getContents()).append(", ");
        outsh1.append(columnB[i].getContents()).append(", ");
        outsh1.append(columnC[i].getContents()).append(", ");
        outsh1.append(columnD[i].getContents()).append(");");

        outsh1.append("\n}\n");
        return outsh1.toString();
    }

    private String stf(Sheet sheet,int flap) {
        StringBuilder outsh1 = new StringBuilder("\nprivate void initStabTrimFlap"+flap+"() {\n\tArrayList<StabTrimSubModel> stabTrimSub = new ArrayList<StabTrimSubModel>();\n\t");
        
        Cell[] column1 = sheet.getColumn(1);
        int j = 1;
        while (j < column1.length) {
            Cell[] row = sheet.getRow(j);
            int i = 2;
            outsh1.append("ArrayList<StabTrimSubSubModel> stabMacs").append(j).append(" = new ArrayList<StabTrimSubSubModel>();\n\t");
            while (i < row.length) {
                outsh1.append("stabMacs").append(j).append(".add(new StabTrimSubSubModel(");
                outsh1.append(ParserBaz.doubleWithDot2(row[i].getContents())).append(", ");
                outsh1.append(ParserBaz.doubleWithDot(row[i + 1].getContents())).append("));\n\t");
                i = i + 2;
            }
            outsh1.append("stabTrimSub.add(new StabTrimSubModel(");
            outsh1.append("\"").append(row[0].getContents()).append("\", ");
            outsh1.append(row[1].getContents()).append(", stabMacs").append(j).append("));\n\t");
            j++;
        }
        outsh1.append("stabTrimFlap").append(flap).append("Data = new StabTrimModel(stabTrimSub);\n}\n");
        return outsh1.toString();
    }

    private String idealtrim(Sheet sheet) {
        StringBuilder outsh1 = new StringBuilder("\nprivate void initIdealTrim() {\n\tidealTrimData = new ArrayList<IdealTrimModel>();\n\t");
        Cell[] columnA = sheet.getColumn(0);
        Cell[] columnB = sheet.getColumn(1);
        CellType empty = CellType.EMPTY;
        int i = 1;
        while (i < columnA.length) {
            outsh1.append("idealTrimData.add(new IdealTrimModel(");
            outsh1.append(columnA[i].getContents()).append(", ");
            outsh1.append(columnB[i].getContents()).append("));\n\t");
            i++;
        }
        outsh1.append("\n}\n");
        return outsh1.toString();
    }

    public String header(int number) {
        StringBuilder outsh1 = new StringBuilder("package samoloty.data;\n\nimport java.util.ArrayList;\nimport samoloty.model.IDataModel;\nimport samoloty.model.models.AcRegModel;");
        outsh1.append("import samoloty.model.models.ConfigurationModel;\nimport samoloty.model.models.ConstModel;\nimport samoloty.model.models.CrewModel;");
        outsh1.append("import samoloty.model.models.FuelModel;\nimport samoloty.model.models.HoldsModel;\nimport samoloty.model.models.IdealTrimModel;");
        outsh1.append("import samoloty.model.models.LimitLawModel;\nimport samoloty.model.models.LimitTowModel;\nimport samoloty.model.models.LimitTowffModel;");
        outsh1.append("import samoloty.model.models.LimitZfwModel;\nimport samoloty.model.models.LimitZfwffModel;\nimport samoloty.model.models.MaxWeightsModel;\n");
        outsh1.append("import samoloty.model.models.PantryModel;\nimport samoloty.model.models.PaxModel;\nimport samoloty.model.models.StabTrimModel;\n");
        outsh1.append("import samoloty.model.models.StabTrimSubModel;\nimport samoloty.model.models.StabTrimSubSubModel;\n");
        outsh1.append("public class DataBase").append(number).append(" extends CommonDataBase implements IDataModel {\n\t public DataBase").append(number).append("() {\n\t super();\n\t initData();\n }\n\t");
        outsh1.append("public ArrayList<String> getAcReg() {\n\t return acRegData;\n }\n\t");
        outsh1.append("public ArrayList<ConfigurationModel> getConfiguration() {\n\t return configuration;\n }\n\t");
        outsh1.append("public ConstModel getConst() {\n\t return constData;\n }\n\t");
        outsh1.append("public ArrayList<CrewModel> getCrew() {\n\t return crewData;\n }\n\t");
        outsh1.append("public ArrayList<FuelModel> getFuel() {\n\t return fuelData;\n }\n\t");
        outsh1.append("public ArrayList<HoldsModel> getHolds() {\n\t return holdsData;\n }\n\t");
        outsh1.append("public ArrayList<LimitLawModel> getLimitLaw() {\n\t return limitLawData;\n }\n\t");
        outsh1.append("public ArrayList<LimitTowModel> getLimitTow() {\n\t return limitTowData;\n }\n\t");
        outsh1.append("public ArrayList<LimitTowffModel> getLimitTowff() {\n\t return limitTowffData;\n }\n\t");
        outsh1.append("public ArrayList<LimitZfwModel> getLimitZfw() {\n\t return limitZfwData;\n }\n\t");
        outsh1.append("public ArrayList<LimitZfwffModel> getLimitZfwff() {\n\t return limitZfwffData;\n }\n\t");
        outsh1.append("public MaxWeightsModel getMaxWeights() {\n\t return maxWeightsData;\n }\n\t");
        outsh1.append("public PantryModel getPantry() {\n\t return pantryData;\n }\n\t");
        outsh1.append("public ArrayList<PaxModel> getPax() {\n\t return paxData;\n }\n\t");
        outsh1.append("public StabTrimModel getStabTrimFlap1() {\n\t return stabTrimFlap1Data;\n }\n\t");
        outsh1.append("public StabTrimModel getStabTrimFlap2() {\n\t return stabTrimFlap2Data;\n }\n\t");
        outsh1.append("public StabTrimModel getStabTrimFlap3() {\n\t return stabTrimFlap3Data;\n }\n\t");
        outsh1.append("public StabTrimModel getStabTrimFlap4() {\n\t return stabTrimFlap4Data;\n }\n\t");
        outsh1.append("public ArrayList<IdealTrimModel> getIdealTrim() {\n\t return idealTrimData;\n }\n\t");
        outsh1.append("private void initData() {\n\t initAcReg();\n\t initConst();\n\t initCrew();\n\t initConfiguratin();\n\t initFuel();\n\t initHolds();\n\t initLimitLaw();\n\t initLimitTow();\n\t initLimitTowff();\n\t initLimitZfw();\n\t initLimitZfwff();\n\t initPantry();\n\t initPax();\n\t initMaxWeights();\n\t initStabTrimFlap1();\n\t initStabTrimFlap2();\n\t initStabTrimFlap3();\n\t initStabTrimFlap4();\n\t initIdealTrim();\n\t }");
        return outsh1.toString();
    }

    public String footer() {
        StringBuilder outsh1 = new StringBuilder("private ArrayList<String> acRegData;\n\t private ConstModel constData;\n\t private ArrayList<CrewModel> crewData;\n\t private ArrayList<ConfigurationModel> configuration;\n\t private ArrayList<FuelModel> fuelData;\n\t private ArrayList<HoldsModel> holdsData;\n\t private ArrayList<LimitZfwModel> limitZfwData;\n\t private ArrayList<LimitTowModel> limitTowData;\n\t private ArrayList<LimitLawModel> limitLawData;\n\t private ArrayList<LimitZfwffModel> limitZfwffData;\n\t private ArrayList<LimitTowffModel> limitTowffData;\n\t private PantryModel pantryData;\n\t private ArrayList<PaxModel> paxData;\n\t private MaxWeightsModel maxWeightsData;\n\t private StabTrimModel stabTrimFlap1Data;\n\t private StabTrimModel stabTrimFlap2Data;\n\t private StabTrimModel stabTrimFlap3Data;\n\t private StabTrimModel stabTrimFlap4Data;\n\t private ArrayList<IdealTrimModel> idealTrimData;\n}");
        return outsh1.toString();
    }

    public static String doubleWithDot(String value) {
        return value.replace(",", ".");
    }

    public static String doubleWithDot2(String value) {
        value = value.replace(",", ".");
        Double d = Double.parseDouble(value);
        return Integer.toString(d.intValue());
    }
}
